package cn.itcast.order.service;

import cn.itcast.order.client.UserClient;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
/*@DefaultProperties(defaultFallback = "allFallBack")*/
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserClient userClient;
/*    @Autowired
    private RestTemplate restTemplate;*/
/*    @HystrixCommand*/
    public String queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
      /*  User user = restTemplate.getForObject("http://userservice:8081/user/" + order.getUserId(), User.class);*/
        User user = userClient.queryById(order.getUserId());
        order.setUser(user);
        // 4.返回
        return JSON.toJSONString(order);
    }
    //返回值，参数必须完全一致
    public String queryOrderByIdFallBack(Long orderId){
        return "服务器忙，请重试！！！";
    }

    public String allFallBack(){

        return "服务器太太太太太太太太太太太太忙，请重试！！！";
    }
}
